package com.youthfilmic.yuejian.service;

import com.youthfilmic.yuejian.conn.QiniuConn;
import com.youthfilmic.yuejian.dao.AccountDao;
import com.youthfilmic.yuejian.dao.UserDao;
import com.youthfilmic.yuejian.domain.Account;
import com.youthfilmic.yuejian.domain.User;
import com.youthfilmic.yuejian.util.OSSUtil;
import com.youthfilmic.yuejian.util.QiniuAPI;
import com.youthfilmic.yuejian.util.SaveImageFromUrlThread;
import com.youthfilmic.yuejian.util.SpringThreadPool;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;

/**
 * Created by youweixi on 15/10/7.
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;
    @Resource
    private AccountDao accountDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public User createNewUser(String openId,int openIdFrom , String unionId, String nickName, String img, int sex , String phone) throws Exception {
        //创建新的用户
        User user = new User();
        //我们的平台规定1是男性，0是女性
        if (sex == 1 || sex == 0){
            user.setSex(User.SEX_BOY);
        }else{
            user.setSex(User.SEX_GIRL);
        }
        user.setCreateData(new Date());
        user.setNickName(nickName);
        user.setOpenId(openId);
        user.setUnionId(unionId);
        user.setOpenIdFrom(openIdFrom);
        user.setPhone(phone);
        //线程处理用户的图片(附用法)
        user.setPicUrl(QiniuAPI.fetch(img, QiniuConn.BUCKET, QiniuAPI.getFileKey( 0 , 1)));
        userDao.save(user);
        //创建新资金账户
        Account account = new Account();
        account.setSum(0);
        account.setUserId(user.getId());
        accountDao.save(account);
        return user;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Account getAccount(Long userId) {
        Account a = accountDao.findByUserId(userId);
        if (a == null){
            //生成新账户
            Account account = new Account();
            account.setSum(0);
            account.setUserId(userId);
            accountDao.save(account);
            return account;
        }else{
            return a;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateUserInfo(Long userId, User user) {
        User u = userDao.findOne(userId);
        u.setBirthday(user.getBirthday());
        u.setCity(user.getCity());
        u.setCompany(user.getCompany());
        u.setEmail(user.getEmail());
        u.setInstruction(user.getInstruction());
        u.setJobYears(user.getJobYears());
        u.setPhone(user.getPhone());
        u.setPresenter(user.getPresenter());
        u.setRealName(user.getRealName());
        u.setReference(user.getReference());
        u.setProfession(user.getProfession());
        if (user.getImg() != null) u.setImg(user.getImg());
        if (user.getPicUrl() != null) u.setPicUrl(user.getPicUrl());
        userDao.save(u);
    }
}
